package cn.com.yusys.yusp.mobile.service;

import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.mobile.repository.mapper.IntegralMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IntegralService {
    @Resource
    IntegralMapper integralMapper;


    public Integer addIntegral(Map map){
        Map<String, Object> ruleMap = null;
        ruleMap = integralMapper.getIntegralRule(map);
        Integer userIntegralScore = integralMapper.getUserIntegralScore(map);
        Integer integralScore =  Integer.valueOf((String) ruleMap.get("integralScore"));
        Integer integralCapping = Integer.valueOf((String) ruleMap.get("integralCapping"));
        if (userIntegralScore==null ){
            map.put("integralTotalScore",integralScore);
            return integralMapper.addUserIntegralScore(map);
        }else if(userIntegralScore.equals(integralCapping)){
            return null;
        }else{
            map.put("integralTotalScore",integralScore+userIntegralScore);
            return integralMapper.updateUserIntegralScore(map);
        }
    }

    public Map<String, Object> getUserIntegralScore(QueryModel queryModel){
        String userCurrentTotalIntegralScore = integralMapper.getUserCurrentTotalIntegralScore(queryModel);
        String userTotalIntegralScore = integralMapper.getUserTotalIntegralScore(queryModel);
        Map<String, Object> integralScore = new HashMap<>();
        integralScore.put("userCurrentTotalIntegralScore",userCurrentTotalIntegralScore == null ? 0 :userCurrentTotalIntegralScore);
        integralScore.put("userTotalIntegralScore",userTotalIntegralScore == null ? 0 : userTotalIntegralScore);
        return integralScore;
    }

}
